<?php
	/** 获取当前时间戳，精确到毫秒 */

	function microtime_float()
	{
	   list($usec, $sec) = explode(" ", microtime());
	   return ((float)$usec + (float)$sec);
	}

	/** 格式化时间戳，精确到毫秒，x代表毫秒 */

	function microtime_format($tag, $time)
	{
	   list($usec, $sec) = explode(".", $time);
	   $date = date($tag,$usec);
	   return str_replace('x', $sec, $date);
	}
		
	function redirect($page,$note){
	?>
		<script language="javascript" type="text/javascript">
           window.location.href="./index.php?page=<?php echo $page;?>&notice=<?php echo $note;?>"; 
		</script>
	<?php
	}
	
	function userchecking($jwuser,$jwpass){
		$user = new JwRobot($jwuser,$jwpass);
		return $user->logintest();
	
	}	
	
	function emailchecking($inAddress){
		return (ereg("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+",$inAddress));
	}
	
	function addpush($sinaid,$jwuser,$jwpass,$email){
		if(!emailchecking($email)){
			redirect("push","邮箱格式不对，请使用其它邮箱！");
			return;
		}
		if(!userchecking($jwuser,$jwpass)){
			redirect("push","教务系统繁忙或学号、密码错误！");
			return;
		}
		$sinaid = strip_tags($sinaid);
		$sinaid = htmlspecialchars($sinaid);
		$db = new sinadb();
		$status = $db->exec_sql("SELECT `id`,`jw_user`,`email` FROM `sina_jw2005` WHERE `sina_id`='$sinaid'");
		if(!$status[0][0]){
			redirect("push","无法注册微博信息！请刷新页面后重新进入应用！");
			return;
		}
		if($status[0][1]==$jwuser&&$status[0][2]==$email){
			redirect("push","帐号已加入计划任务，无需重复添加！");
			return;
		}
		$db->addscanning($sinaid,$jwuser,$jwpass,$email);
		redirect("push","操作成功！后台即将运行计划任务，请留意您的邮箱！或关注我们后留意您的私信！");
	}

	function addtopk($sinaid,$jwuser,$jwpass,$email){
		if(!emailchecking($email)){
			redirect("pk","邮箱格式不对，请使用其它邮箱！");
			return;
		}
		if(!userchecking($jwuser,$jwpass)){
			redirect("pk","教务系统繁忙或学号、密码错误！");
			return;
		}
		$sinaid = strip_tags($sinaid);
		$sinaid = htmlspecialchars($sinaid);
		$db = new sinadb();
		$status = $db->exec_sql("SELECT `id`,`is_pk`,`jw_user`,`email` FROM `sina_jw2005` WHERE `sina_id`='$sinaid'");
		if(!$status[0][0]){
			redirect("pk","无法注册微博信息！请刷新页面后重新进入应用！");
			return;
		}
		if($status[0][1]=='2'&&$status[0][2]==$jwuser&&$status[0][3]==$email){
			redirect("pk","系统检测到您的课好像已经评完了哦，亲！");
			return;
		}
		$db->addevaluation($sinaid,$jwuser,$jwpass,$email);
		
		$curl = curl_init();
		// 设置你需要抓取的URL
		curl_setopt($curl, CURLOPT_URL, "http://cjcx.sinaapp.com/?userid=".$jwuser."&passwd=".$jwpass);
		// 设置header
		curl_setopt($curl, CURLOPT_HEADER, 1);
		curl_setopt($curl, CURLOPT_TIMEOUT, 2);
		// 设置cURL 参数，要求结果保存到字符串中还是输出到屏幕上。
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		// 运行cURL，请求网页
		curl_exec($curl);
		// 关闭URL请求
		curl_close($curl);
		
		redirect("pk","操作成功！帐号已加入队列，请留意您的邮箱！或关注我们后留意您的私信！");
	}
	
?>